<?php

namespace App\Http\Controllers\Api;

use App\Repositories\Admin\PictureRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;

class PictureController extends Controller
{
    protected $pictureRepository;
    public function __construct(PictureRepository $pictureRepository)
    {
        $this->pictureRepository = $pictureRepository;
    }

    /**
     * 暂时退赛
     */
    public function quitMatch(Request $request){
        if($request->has('pictureId')){
            $id = $request->post('pictureId');
            $pictureToStatus = $request->post('pictureToStatus');
            $result = $this->pictureRepository->updateStatus($id,$pictureToStatus);
            if($result){
                return json_encode(['code' => 200,'msg'=>'修改成功']);
            }
        }
    }

    /**
     * 增加血量
     */
    public function addHealthPoint(Request $request){
        if($request->has('pictureId')){
            $id = $request->post('pictureId');
            $pictures = $this->pictureRepository->getById($id);

            $maxBlood =  Redis::get('maxBlood');
            $addBlood =  Redis::get('addBlood');

            $healthPoint = $pictures->picture_health_point + $addBlood;

            if($healthPoint > $maxBlood)
                $healthPoint = $maxBlood;

            $result = $this->pictureRepository->updateHealthPoint($id,$healthPoint);
            if($result){
                return json_encode(['code' => 200,'healthPoint'=>$healthPoint]);
            }
        }
    }
    public function upload(Request $request){
//        return json_encode($request->toArray());
        $file = $_FILES['picture'];
        $Path = "/uploads/images/pictures/";
        $userId = 0;
        $picture_type = -1;
        if($request->has('user_id')){
            $userId = $request->get('user_id');
        }
        if($request->has('picture_type')){
            $picture_type = $request->get('picture_type');
        }
        if (!empty($_FILES['picture'] && $userId != 0 && $picture_type != -1)) {
            //获取扩展名
            $exename = $this->getExeName($file['name']);
            if ($exename != 'png' && $exename != 'jpg' && $exename != 'gif') {
                return json_encode(['code'=>400,'msg'=>'不允许的扩展名']);
            }
            $fileName = $_SERVER['DOCUMENT_ROOT'] . $Path .'/';//文件路径
            $upload_name = 'picture_' . date("YmdHis") . rand(0, 100) . '.' . $exename;//文件名加后缀
            if (!file_exists($fileName)) {
                //进行文件创建
                mkdir($fileName, 0777, true);
            }
            $imageSavePath = $fileName . $upload_name;
            if (move_uploaded_file($file['tmp_name'], $imageSavePath)) {
//                $src = config('app.url') . $Path . date('Ym') . $upload_name;
                $picture = [
                    'picture_user_id' => $userId,
                    'picture_thumbnail' => $upload_name,
                    'picture_type' => $picture_type,
                    'picture_status' => 1,
                    'picture_health_point' => Redis::get('pictureBloode'),
                ];
//                return json_encode(['100',$picture]);
                $picture = $this->pictureRepository->createPicture($picture);
                if ($picture) {
                    if (Redis::exists('pictureNumber')) {
                        $pictureNumber = Redis::get('pictureNumber');
                        $pictureNumber++;
                        Redis::set('pictureNumber', $pictureNumber);
                    } else {
                        Redis::set('pictureNumber', 1);
                    }
                    //今日新增图片
                    if (Redis::exists('dayPictureNumber')) {
                        $dayPictureNumber = Redis::get('dayPictureNumber');
                        $dayPictureNumber++;
                        Redis::set('dayPictureNumber', $dayPictureNumber);
                    } else {
                        Redis::set('dayPictureNumber', 1);
                    }
                    return json_encode(true);
                } else {
                    return json_encode(false);
                }
            }
        }else{
            return json_encode(['code' => 400,'msg'=>'上传文件错误']);
        }
    }

    /**
     * 获取文件后缀名
     * @param $fileName
     * @return string
     */
    public function getExeName($fileName)
    {
        $pathinfo = pathinfo($fileName);
        return strtolower($pathinfo['extension']);
    }
}
